<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <input id="id" name="id" type="hidden" value="-1">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">手机</label>
            <div class="layui-input-block">
                <input id="phone" type="text" name="phone" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input id="name" type="text" name="name" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">地址</label>
            <div class="layui-input-block">
                <input id="address" type="text" name="address" lay-verify="required" lay-reqtext="地址不能为空" placeholder="请输入地址" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="男" title="男" checked="">
                <input type="radio" name="sex" value="女" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input id="email" type="text" name="email" placeholder="请输入邮箱" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script src="../../js/common.js"></script>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        let isEdit = false
        let user = JSON.parse(localStorage.getItem('user1'))
        if (user) {
            isEdit = true
            $('#id').val(user.id)
            $('#name').val(user.name)
            $('#phone').val(user.phone)
            $('#count').val(user.count)
            $('#email').val(user.email)
            $('#address').val(user.address)
            let len = $("input[type='radio']").length
            for (let i = 0; i < len; i++) {
                let r = $("input[type='radio']").eq(i)
                console.log(r)
                if (r.val() == user.sex) {
                    r.attr('checked', true)
                }
            }
        }

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();
        localStorage.removeItem('user1')
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if (isEdit) {
                $.ajax({
                    url: '../user/update',
                    method: "POST",
                    dataType: 'json',
                    data: data.field,
                    success: function (resp) {
                        var index = layer.alert('保存成功', function () {
                            // 关闭弹出层
                            layer.close(index);
                            layer.close(parentIndex);
                            window.location.reload();
                        });
                    }
                })
            }
            return false;
        });

    });
</script>